#This script executes regressions to estimate how the
#association between first-generation prenatal exposure to nonattainment 
#and second-generation college attendance varies between biological and adopted/step- children.


rm(list=ls())
gc()
library(reldist)
library(ineq)
library(stargazer)
library(data.table)
library(dplyr)
library(dtplyr)
library(ggplot2)
library(stringdist)
library(lfe)
library(haven)
library(readr)
library(broom)
library(tidylog)
library(rdrobust)

# Set the working directory
setwd("/projects/opp_env/caa1970/")

# Source external R scripts for custom functions
source("Code/Child_Outcomes/child_outcomes_functions.R")
source("Code/rounding.r")

# Load the second-generation analysis dataset
load("Data/second_gen_college_analysis_data_jpemicro.rda")

regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm),!is.na(incollege), !is.na(parent_race), !is.na(PI_PC))%>%group_by(pik) %>% summarise(n=n()) %>% group_by(n) %>% summarise(n1=n())

Table_3 <- data.frame()

#Panel A, column 1
Tab3_A1 <- felm(adopted_or_stepchild~ #LHS 
                  factor(parent_race)+factor(parent_female)+PI_PC*year_trend+
                  PI_PC*I(year_trend^2) +epop*year + total_transfers*year_trend + 
                  county_pop*year_trend + epop*I(year_trend^2) + 
                  total_transfers*I(year_trend^2) + county_pop*I(year_trend^2) +
                  tmax_fullterm+ tmin_fullterm+prec_fullterm+anyprec_fullterm | #Controls
                  state_year+county_factor+year_factor+month+
                  survey_year_by_year | #Fixed Effects
                  (all_fullterm~nonattainment_infant) | #IV spec
                  county_factor, #Cluster
                data=regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm),!is.na(incollege), !is.na(parent_race), !is.na(PI_PC))) 
summary(Tab3_A1)
Tab3_A1$N
temp_n<-  rounding_rules_counts(Tab3_A1$N)
temp_ctrl <- signif(mean((regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), !is.na(incollege),!is.na(parent_race), !is.na(PI_PC),nonattainment_infant == 0))$adopted_or_stepchild, na.rm=T ),4)
temp_f <-  signif(Tab3_A1$stage1$iv1fstat[[1]]["F"],4)
temp_results <- tidy(Tab3_A1) %>% filter(term == "`all_fullterm(fit)`" ) %>% #tidy regression, keep coef of interest
  mutate_each(funs(signif(.,4)), -term) %>% #round appropriately
  mutate(N=temp_n, ctrl_mean=temp_ctrl, fstat = temp_f,  table = 3, column ="A1" ) #add variables for N and control mean, plus index the table number and column
Table_3 <- bind_rows(Table_3, temp_results) #Appended to big dataset

#Panel A, Column 2

Tab3_A2 <- felm(incollege~ #LHS 
                  factor(parent_race)+factor(parent_female)+PI_PC*year_trend+
                  PI_PC*I(year_trend^2) +epop*year + total_transfers*year_trend + 
                  county_pop*year_trend + epop*I(year_trend^2) + 
                  total_transfers*I(year_trend^2) + county_pop*I(year_trend^2) +
                  tmax_fullterm+ tmin_fullterm+prec_fullterm+anyprec_fullterm | #Controls
                  state_year+county_factor+year_factor+month+
                  survey_year_by_year | #Fixed Effects
                  (all_fullterm~nonattainment_infant) | #IV spec
                  county_factor, #Cluster
                data=regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), adopted_or_stepchild==0, !is.na(parent_race), !is.na(PI_PC))) 

summary(Tab3_A2)
Tab3_A2$N
temp_n<-  rounding_rules_counts(Tab3_A2$N)
temp_ctrl <- signif(mean((regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), adopted_or_stepchild==0, !is.na(incollege),!is.na(parent_race), !is.na(PI_PC),nonattainment_infant == 0))$incollege, na.rm=T ),4)
temp_f <-  signif(Tab3_A2$stage1$iv1fstat[[1]]["F"],4)
temp_results <- tidy(Tab3_A2) %>% filter(term %in% c("`all_fullterm(fit)`") ) %>% #tidy regression, keep coef of interest
  mutate_each(funs(signif(.,4)), -term) %>% #round appropriately
  mutate(N=temp_n, ctrl_mean=temp_ctrl, fstat = temp_f,  table = 3, column ="A2" ) #add variables for N and control mean, plus index the table number and column
Table_3 <- bind_rows(Table_3, temp_results) #Appended to big dataset

#Panel A, Column 3

Tab3_A3 <- felm(incollege~ #LHS 
                  factor(parent_race)+factor(parent_female)+PI_PC*year_trend+
                  PI_PC*I(year_trend^2) +epop*year + total_transfers*year_trend + 
                  county_pop*year_trend + epop*I(year_trend^2) + 
                  total_transfers*I(year_trend^2) + county_pop*I(year_trend^2) +
                  tmax_fullterm+ tmin_fullterm+prec_fullterm+anyprec_fullterm | #Controls
                  state_year+county_factor+year_factor+month+
                  survey_year_by_year | #Fixed Effects
                  (all_fullterm~nonattainment_infant) | #IV spec
                  county_factor, #Cluster
                data=regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), adopted_or_stepchild==1, !is.na(parent_race), !is.na(PI_PC))) 

summary(Tab3_A3)
Tab3_A3$N
temp_n<-  rounding_rules_counts(Tab3_A3$N)
temp_ctrl <- signif(mean((regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), adopted_or_stepchild==1,!is.na(incollege),!is.na(parent_race), !is.na(PI_PC),nonattainment_infant == 0))$incollege, na.rm=T ),4)
temp_f <-  signif(Tab3_A3$stage1$iv1fstat[[1]]["F"],4)
temp_results <- tidy(Tab3_A3) %>% filter(term %in% c("`all_fullterm(fit)`") ) %>% #tidy regression, keep coef of interest
  mutate_each(funs(signif(.,4)), -term) %>% #round appropriately
  mutate(N=temp_n, ctrl_mean=temp_ctrl, fstat = temp_f,  table = 3, column ="A3" ) #add variables for N and control mean, plus index the table number and column
Table_3 <- bind_rows(Table_3, temp_results) #Appended to big dataset

#Panel B, column 1
Tab3_B1 <- felm(adopted_or_stepchild~ #LHS 
                  factor(parent_race)+factor(parent_female)+PI_PC*year_trend+
                  PI_PC*I(year_trend^2) +epop*year + total_transfers*year_trend + 
                  county_pop*year_trend + epop*I(year_trend^2) + 
                  total_transfers*I(year_trend^2) + county_pop*I(year_trend^2) +
                  tmax_fullterm+ tmin_fullterm+prec_fullterm+anyprec_fullterm + nonattainment_infant| #Controls
                  state_year+county_factor+year_factor+month+
                  survey_year_by_year | #Fixed Effects
                  0 | #IV spec
                  county_factor, #Cluster
                data=regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm),!is.na(incollege), !is.na(parent_race), !is.na(PI_PC))) 
summary(Tab3_B1)
Tab3_B1$N
temp_n<-  rounding_rules_counts(Tab3_B1$N)
temp_ctrl <- signif(mean((regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), !is.na(incollege),!is.na(parent_race), !is.na(PI_PC),nonattainment_infant == 0))$adopted_or_stepchild, na.rm=T ),4)
temp_results <- tidy(Tab3_B1) %>% filter(term == "nonattainment_infant" ) %>% #tidy regression, keep coef of interest
  mutate_each(funs(signif(.,4)), -term) %>% #round appropriately
  mutate(N=temp_n, ctrl_mean=temp_ctrl,  table = 3, column ="B1" ) #add variables for N and control mean, plus index the table number and column
Table_3 <- bind_rows(Table_3, temp_results) #Appended to big dataset

#Panel B, column 2
Tab3_B2 <- felm(incollege~ #LHS 
                  factor(parent_race)+factor(parent_female)+PI_PC*year_trend+
                  PI_PC*I(year_trend^2) +epop*year + total_transfers*year_trend + 
                  county_pop*year_trend + epop*I(year_trend^2) + 
                  total_transfers*I(year_trend^2) + county_pop*I(year_trend^2) +
                  tmax_fullterm+ tmin_fullterm+prec_fullterm+anyprec_fullterm + nonattainment_infant | #Controls
                  state_year+county_factor+year_factor+month+
                  survey_year_by_year | #Fixed Effects
                  0 | #IV spec
                  county_factor, #Cluster
                data=regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), adopted_or_stepchild==0, !is.na(parent_race), !is.na(PI_PC))) 

summary(Tab3_B2)

Tab3_B2$N
temp_n<-  rounding_rules_counts(Tab3_B2$N)
temp_ctrl <- signif(mean((regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), adopted_or_stepchild==0,!is.na(incollege),!is.na(parent_race), !is.na(PI_PC),nonattainment_infant == 0))$incollege, na.rm=T ),4)
temp_results <- tidy(Tab3_B2) %>% filter(term %in% c("nonattainment_infant") ) %>% #tidy regression, keep coef of interest
  mutate_each(funs(signif(.,4)), -term) %>% #round appropriately
  mutate(N=temp_n, ctrl_mean=temp_ctrl,   table = 3, column ="B2" ) #add variables for N and control mean, plus index the table number and column
Table_3 <- bind_rows(Table_3, temp_results) #Appended to big dataset


Tab3_B3 <- felm(incollege~ #LHS 
                  factor(parent_race)+factor(parent_female)+PI_PC*year_trend+
                  PI_PC*I(year_trend^2) +epop*year + total_transfers*year_trend + 
                  county_pop*year_trend + epop*I(year_trend^2) + 
                  total_transfers*I(year_trend^2) + county_pop*I(year_trend^2) +
                  tmax_fullterm+ tmin_fullterm+prec_fullterm+anyprec_fullterm +nonattainment_infant | #Controls
                  state_year+county_factor+year_factor+month+
                  survey_year_by_year | #Fixed Effects
                  0 | #IV spec
                  county_factor, #Cluster
                data=regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm), adopted_or_stepchild==1, !is.na(parent_race), !is.na(PI_PC))) 

summary(Tab3_B3)

Tab3_B3$N
temp_n<-  rounding_rules_counts(Tab3_B3$N)
temp_ctrl <- signif(mean((regdata %>% filter(year%in%1969:1975, !is.na(all_fullterm),adopted_or_stepchild==1, !is.na(incollege),!is.na(parent_race), !is.na(PI_PC),nonattainment_infant == 0))$incollege, na.rm=T ),4)
temp_results <- tidy(Tab3_B3) %>% filter(term %in% c("nonattainment_infant") ) %>% #tidy regression, keep coef of interest
  mutate_each(funs(signif(.,4)), -term) %>% #round appropriately
  mutate(N=temp_n, ctrl_mean=temp_ctrl,   table = 3, column ="B3" ) #add variables for N and control mean, plus index the table number and column
Table_3 <- bind_rows(Table_3, temp_results) #Appended to big dataset

write_csv(Table_3, "/projects/opp_env/caa1970/JPE_Micro/Output/Table 3/Table_3.csv")
